﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using Store.Web.Tools;

namespace Store.Web.Models
{
    public class StoreDbContext : DbContext 
    {
        public StoreDbContext(string connectionString)
            : base(connectionString)
        {
            if (!Database.Exists())
            {
                Database.Create();
            }
        }

        public IDbSet<Product> Products
        {
            get { return Set<Product>(); }
        }

        public IDbSet<Order> Orders
        {
            get { return Set<Order>(); }
        }

        public IDbSet<Address> Addresses
        {
            get { return Set<Address>(); }
        }

        public IDbSet<Customer> Customers
        {
            get { return Set<Customer>(); }
        }

        public IDbSet<PushSubscription> PushSubscriptions
        {
            get { return Set<PushSubscription>(); }
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Order>()
                .HasMany<Product>(c => c.Products)
                .WithMany();

            modelBuilder.Entity<Order>()
                .HasRequired(c => c.Customer);

            modelBuilder.Entity<Customer>()
                .HasMany<Address>(c => c.Addresses)
                .WithMany();

            

            base.OnModelCreating(modelBuilder);
        }
    }
}